﻿/*
* 创建者：LIYINFAI
* 创建时间：2023/10/24 9:35:26
* 描述：
* --------------------------------------
* 修改说明：
* --------------------------------------
*/
using System;
using System.IO;
using System.Threading;

namespace CommonLibrary
{
    /// <summary>
    /// 产品数据文件管理器
    /// </summary>
    public class ProductFileManager
    {
        /// <summary>
        /// 生产数据文件前缀名
        /// </summary>
        public const string FILE_PREV_NAME = "氦检";
        /// <summary>
        /// 生产数据库前缀名
        /// </summary>
        public const string DB_PREV_NAME = "products";
        /// <summary>
        /// 生产数据是否阶段性存储
        /// </summary>
        public static bool IsProductIntervalSave
        {
            get
            {
                return BusinessConfig.Instance().IsProductIntervalSave;
            }
        }
        /// <summary>
        /// 阶段性存储线程
        /// </summary>
        private static Thread FILE_THREAD = null;
        /// <summary>
        /// 是否停止
        /// </summary>
        private static bool STOP = true;
        /// <summary>
        /// 阶段性存储操作锁
        /// </summary>
        private static object FILE_LOCK = new object();
        /// <summary>
        /// 启动阶段性存储线程
        /// </summary>
        public static void Start()
        {
            if (IsProductIntervalSave)
            {
                STOP = false;
                FILE_THREAD = new Thread(new ThreadStart(() =>
                {
                    while (!STOP)
                    {
                        lock (FILE_LOCK)
                        {
                            try
                            {
                                #region file name
                                DateTime now = DateTime.Now;
                                DateTime today = now.Date;
                                DateTime tommorrow = today.AddDays(1);
                                DateTime yesterday = today.AddDays(-1);
                                string startTime = $"{today.ToString("yyyy-MM-dd")} 08:00:00";
                                string endTime = $"{today.ToString("yyyy-MM-dd")} 20:00:00";
                                string dir = today.ToString("yyyy-MM-dd");
                                string fileName = $"{FILE_PREV_NAME}_{AppBasicSetting.Instance().EquipmentCode}_{today.ToString("yyyyMMdd")}.xlsx";
                                int hours = now.Hour;
                                if (hours < 9)
                                {
                                    fileName = $"{FILE_PREV_NAME}_{AppBasicSetting.Instance().EquipmentCode}_{yesterday.ToString("yyyyMMdd")}_晚班.xlsx";
                                    startTime = $"{yesterday.ToString("yyyy-MM-dd")} 20:00:01";
                                    endTime = $"{today.ToString("yyyy-MM-dd")} 07:59:59";
                                    dir = yesterday.ToString("yyyy-MM-dd");
                                }
                                else if (hours < 21)
                                {
                                    fileName = $"{FILE_PREV_NAME}_{AppBasicSetting.Instance().EquipmentCode}_{today.ToString("yyyyMMdd")}_白班.xlsx";
                                    startTime = $"{today.ToString("yyyy-MM-dd")} 08:00:00";
                                    endTime = $"{today.ToString("yyyy-MM-dd")} 20:00:00";
                                    dir = today.ToString("yyyy-MM-dd");
                                }
                                else
                                {
                                    fileName = $"{FILE_PREV_NAME}_{AppBasicSetting.Instance().EquipmentCode}_{today.ToString("yyyyMMdd")}_晚班.xlsx";
                                    startTime = $"{today.ToString("yyyy-MM-dd")} 20:00:01";
                                    endTime = $"{tommorrow.ToString("yyyy-MM-dd")} 07:59:59";
                                    dir = today.ToString("yyyy-MM-dd");
                                }
                                #endregion
                                string filePath = Path.Combine(Path.Combine(CommonHelper.GetBaseDataDirectory(), dir), fileName);
                                var list = ProductSQLiteDb.GetProductInfoAllList(string.Empty, startTime, endTime);
                                if (list != null && list.Count > 0)
                                {
                                    list.Reverse();
                                }
                                CommonHelper.WriteExcel(filePath, list, true);
                            }
                            catch (Exception ex)
                            {
                                LogHelper.Error(ex);
                            }
                        }
                        int max = BusinessConfig.Instance().ProductIntervalSleepTimes * 1000;
                        int start = 1000;
                        while (start < max && !STOP)
                        {
                            Thread.Sleep(1000);
                            start += 1000;
                        }
                    }
                }));
                FILE_THREAD.Start();
            }
        }
        /// <summary>
        /// 停止阶段性存储线程
        /// </summary>
        public static void Stop()
        {
            if (IsProductIntervalSave)
            {
                STOP = true;
            }
        }
    }
}
